package org.libermundi.theorcs.chronicles.services.message.impl;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.libermundi.theorcs.chronicles.dao.InternalEmailDao;
import org.libermundi.theorcs.chronicles.model.Persona;
import org.libermundi.theorcs.chronicles.model.message.Channel;
import org.libermundi.theorcs.chronicles.model.message.TargetRole;
import org.libermundi.theorcs.chronicles.model.message.impl.InternalEmail;
import org.libermundi.theorcs.chronicles.model.message.impl.InternalEmailTarget;
import org.libermundi.theorcs.chronicles.services.ChronicleManager;
import org.libermundi.theorcs.chronicles.services.PersonaManager;
import org.libermundi.theorcs.chronicles.services.message.MessageChainService;
import org.libermundi.theorcs.core.services.impl.AbstractManagerImpl;
import org.libermundi.theorcs.security.model.User;
import org.libermundi.theorcs.security.services.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;



/**
 * Service for internal email service (notifications of new posts, new messages from other personas...)
 * 
 *
 */
@Service("internalEmailService")
@Transactional(rollbackFor = Exception.class, propagation=Propagation.REQUIRED)
public class InternalEmailServiceImpl extends AbstractManagerImpl<InternalEmail,Long> implements MessageChainService<InternalEmail, Persona> {
	
	private static final Logger logger = LoggerFactory.getLogger(InternalEmailServiceImpl.class);
	
	@Autowired
	public InternalEmailServiceImpl(InternalEmailDao messageDao) {
		super();
		setDao(messageDao);
	}

	@Autowired
	private UserManager _userManager;

	@Autowired
	private ChronicleManager _chronicleManager;
	
	@Autowired
	private PersonaManager _personaManager;
	
	/* (non-Javadoc)
	 * @see org.libermundi.theorcs.chronicles.services.message.MessageChainService#process(org.libermundi.theorcs.chronicles.model.messenging.Message, org.libermundi.theorcs.chronicles.model.messenging.Channel, org.libermundi.theorcs.chronicles.model.messenging.Targets)
	 */
	@Override
	public boolean process(InternalEmail message, Channel channel) {
		if(channel!=null && channel.equals(Channel.INT_EMAIL)) {
			message.setSendingDate(GregorianCalendar.getInstance().getTime());
			save(message);
			return true;
		}
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.libermundi.theorcs.core.services.impl.AbstractManagerImpl#initialize()
	 */
	@Override
	public void initialize() {
		if(getAllCount() == 0L) {
			logger.debug("Initializing messaging");
			User player = _userManager.getUserByUsername("user2");
			Persona persoUser2 = _personaManager.getDefaultPersona(player, _chronicleManager.loadLast());
			User player1 = _userManager.getUserByUsername("admin");	
			Persona persoAdmin = _personaManager.getDefaultPersona(player1, _chronicleManager.loadLast());
			User player2 = _userManager.getUserByUsername("user1");	
			Persona perso2 = _personaManager.getDefaultPersona(player2, _chronicleManager.loadLast());
			
			//TODO create a factory
			InternalEmail m0 = new InternalEmail();
			m0.setTitle("title of an internal Email0");
			m0.setBody("body of an internal Email0");
			m0.setSendingDate(GregorianCalendar.getInstance().getTime());
			InternalEmailTarget t0 = new InternalEmailTarget(persoUser2, TargetRole.BY);
			InternalEmailTarget t1 = new InternalEmailTarget(persoAdmin, TargetRole.TO);
			InternalEmailTarget t2 = new InternalEmailTarget(persoAdmin, TargetRole.CC);
			InternalEmailTarget t3 = new InternalEmailTarget(perso2, TargetRole.BCC);
			List<InternalEmailTarget> recipient = new ArrayList<InternalEmailTarget>();
			recipient.add(t0);
			recipient.add(t1);
			recipient.add(t2);
			recipient.add(t3);
			m0.setRecipient(recipient);
			save(m0);
			

			
			InternalEmail m1 = new InternalEmail();
			m1.setTitle("title of an internal Email1");
			m1.setBody("body of an internal Email1");
			m1.setSendingDate(GregorianCalendar.getInstance().getTime());
			InternalEmailTarget t10 = new InternalEmailTarget(persoAdmin, TargetRole.BY);
			InternalEmailTarget t11 = new InternalEmailTarget(persoUser2, TargetRole.TO);
			InternalEmailTarget t12 = new InternalEmailTarget(perso2, TargetRole.CC);
			InternalEmailTarget t13 = new InternalEmailTarget(persoAdmin, TargetRole.BCC);
			List<InternalEmailTarget> recipient1 = new ArrayList<InternalEmailTarget>();
			recipient1.add(t10);
			recipient1.add(t11);
			recipient1.add(t12);
			recipient1.add(t13);
			m1.setRecipient(recipient1);
			
			save(m1);
		}
			
	}

	@Override
	public List<InternalEmail> getEntityMessages(Persona numericId) {
		return ((InternalEmailDao)getDao()).getPersonaMessages(numericId);
	}

	@Override
	public InternalEmail getEntityMessage(long numericId) {
		return load(numericId);
	}
	
}
